package com.uwo.commons.kits;

import java.util.*;

/**
 * 过滤敏感字符
 * @author hao.yan
 */
public class SensitiveKits {

    // 敏感词
    private static final Map<String, Object> sensitiveWords = new HashMap<String, Object>();

    /**
     * 设置敏感词
     * @param keyWordSet
     */
    public static void setSensitiveWords(Set<String> keyWordSet) {
        String key = null;
        Map nowMap = null;
        Map<String, Object> newWorMap = null;
        //迭代keyWordSet
        Iterator<String> iterator = keyWordSet.iterator();
        while(iterator.hasNext()){
            key = iterator.next();    //关键字
            nowMap = sensitiveWords;
            for(int i = 0 ; i < key.length() ; i++){
                char keyChar = key.charAt(i);       //转换成char型
                Object wordMap = nowMap.get(keyChar);       //获取
                if(wordMap != null){        //如果存在该key，直接赋值
                    nowMap = (Map) wordMap;
                } else {     //不存在则，则构建一个map，同时将isEnd设置为0，因为他不是最后一个
                    newWorMap = new HashMap<String,Object>();
                    newWorMap.put("isEnd", false);     //不是最后一个
                    nowMap.put(keyChar, newWorMap);
                    nowMap = newWorMap;
                }
                if(i == key.length() - 1){
                    nowMap.put("isEnd", true);    //最后一个
                }
            }
        }
    }

    /**
     * 替换敏感词
     * @param context
     * @param replace
     * @return
     */
    public String replaceSensitiveWords(String context, String replace){
        String resultTxt = context;
        // 获取所有的敏感词
        Set<String> set = getSensitiveWords(context);
        Iterator<String> iterator = set.iterator();
        String word = null;
        String replaceString = null;
        while (iterator.hasNext()) {
            word = iterator.next();
            replaceString = getReplaceStr(replace, word.length());
            resultTxt = resultTxt.replaceAll(word, replaceString);
        }

        return resultTxt;
    }

    /**
     * 替换字符
     * @param replace
     * @param length
     * @return
     */
    private String getReplaceStr(String replace,int length){
        String replaceStr = replace;
        for(int i = 1 ; i < length ; i++){
            replaceStr += replace;
        }
        return replaceStr;
    }

    /**
     * 验证敏感词
     * @return
     */
    public static boolean validateSensitiveWords(String context){
        Set<String> sensitives = getSensitiveWords(context);
        return (sensitives.size() > 0);
    }

    /**
     * 获取敏感词
     * @param context
     * @return
     */
    public static Set<String> getSensitiveWords(String context){
        char word = 0;
        Set<String> searchs = new HashSet<String>();
        StringBuilder sb = new StringBuilder();
        Map nowMap = sensitiveWords;
        System.out.println(sensitiveWords);
        for(int i = 0; i < context.length() ; i++){
            word = context.charAt(i);
            // 不存在
            if(!nowMap.containsKey(word)) {
                nowMap = sensitiveWords;
                sb = new StringBuilder();
                continue;
            }
            sb.append(word);
            // 获取指定key
            nowMap = (Map) nowMap.get(word);
            // 获取是否最后一个
            boolean isEnd = (Boolean) nowMap.get("isEnd");
            if(isEnd){
                searchs.add(sb.toString());
            }
        }
        return searchs;
    }

}
